procedure mask()
#procedure mask(nome, ver, hor, ext, colavg, colstep, apscatter,lista)

char    nome         {"mask",mode="h",prompt="IBase name for files "}
int     ver          {1,min=1,max=10,mode="h",prompt="Number of vertical mask positions"}
int     hor          {5,min=1,max=10,mode="h",prompt="Number of horizontal mask positions"}
char    ext          {"fits",enum="txt|fits|imh",mode="h",prompt="Filetype of images"}
int     colavg       {1,min=1,max=15,mode="h",prompt="Number of columns to average during fits"}
int     colstep      {1,min=1,max=500,mode="h",prompt="Number of columns to jump from one fit to the next"}
pset    apscatter    
struct  *lista

begin

char curr_mask, sc_mask, base_mask

int i,j 

# iraf doesn't like we use the parameter variables within the script
# so here we make a copy of them to a new variable 
# whose name has an n added to the beginning of the name of each of them
# 
int n_ver, n_hor, n_colavg, n_colstep
char n_nome, n_ext
char com_str

n_ver = ver
n_hor = hor
n_ext = ext
n_nome = nome
n_colavg = colavg
n_colstep = colstep


if (imaccess(nome))
   imdelete(nome,verify=no)
if (access("database/ap"//nome))
   delete("database/ap"//nome,verify=no)

base_mask = nome
for (i=0 ; i<n_ver ; i+=1)
   for (j=0 ; j<n_hor ; j+=1) {
      curr_mask = nome // "-0" // i // "-0" // j  
      sc_mask = nome // ".sc-0" // i // "-0" // j 
      print("CURR, SC",curr_mask,"SCATTERED",sc_mask)
# aperture size must be defined in apresize or set outside by other
# calling routine
      apfind(input=curr_mask)
      if (imaccess(sc_mask))
         imdelete(sc_mask,verify=no)
      aptrace(input=curr_mask)
      # apscatter causes segmentation violations
      # *** I am stuck with this and don't know what to do
      # *** the segmentation violation shows up at weird places, but
      # *** apscatter is the culprit
      # apscatter(input=curr_mask,output=sc_mask)
      # the original code uses sc_mask from this point on
      # *** because apscatter is not working I use this copy here
      # *** to create the sc_mask file which is really not scattering
      # *** corrected :-(
      imcopy(curr_mask,sc_mask,verbose=no)
      # print("Done scattering",curr_mask,nome)
      type("database/ap"//curr_mask,>>"database/ap"//nome)
      if (imaccess(nome))
         imdelete(nome,verify=no)

      if ( (i==0) && (j==0) ){
         if (imaccess(nome)){
            imdelete(nome,verify=no)
         }
      imcopy(sc_mask,nome,verbose=no)
      }
      else{
         imarith(sc_mask,"+",nome,nome)
      }
   }

print "Writing out ifu.conf file")
print(n_hor,>>"ifu.conf")
print(n_ver,>>"ifu.conf")
print(base_mask//".sc",>>"ifu.conf")
print(n_ext,>>"ifu.conf")
print(n_colavg,>>"ifu.conf")
print(n_colstep,>>"ifu.conf")


if (access("lixo091"))
   delete("lixo091")
com_str = "!awk -f ${SOAR_IFU}/awk/order.awk < database/ap" // nome // " > lixo091"
print(com_str)
print(com_str,|cl)
com_str = "!awk -f ${SOAR_IFU}/awk/renumber.awk < lixo091 >! database/ap" // nome
print(com_str,|cl)
delete("lixo091",verify=no)

# counting how many apertures we found in the end
print("Counting apertures ")
if (access("database/Nmask"))
   delete("database/Nmask",verify=no)
com_str = "!grep -c begin database/ap" // nome // "> database/Nmask"
print(com_str)
print(com_str,|cl)

end
